package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {

		//创建集合
		List<JSONObject> list = new ArrayList<>();
		//查询视图中所有的数据
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		//构建数据结构
		Map<Long, List<BaseCategoryView>> map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()){
			//获取数据
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			Long category1Id = entry.getKey();
			List<BaseCategoryView> baseCategoryViews1 = entry.getValue();
			//创建一个一级分类
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",category1Id);
			category1.put("categoryName",baseCategoryViews1.get(0).getCategory1Name());
			// 以二级分类id 分组
			Map<Long, List<BaseCategoryView>> map1 = baseCategoryViews1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
			//声明一个二级分类集合
			List<JSONObject> categoryChildList = new ArrayList<>();
			while (iterator1.hasNext()){
				JSONObject category2 = new JSONObject();
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				List<BaseCategoryView> baseCategoryViews2 = entry1.getValue();
				category2.put("categoryId",category2Id);
				category2.put("categoryName",baseCategoryViews2.get(0).getCategory2Name());
			//获取三级分类数据
				List<JSONObject> category3ChildList = baseCategoryViews2.stream().map(baseCategoryView -> {
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//添加三级分类集合
				category2.put("categoryChild",category3ChildList);
				categoryChildList.add(category2);
			}
			category1.put("categoryChild",categoryChildList);
			list.add(category1);

		}
		return list;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {

		return baseAttributeMapper.findAttribute(category1Id);
	}
}
